# training/curriculum_learning.py
from typing import Dict, List
import numpy as np

class CurriculumManager:
    """课程学习管理器 - 逐步增加训练难度"""
    
    def __init__(self, settings: Dict):
        self.settings = settings
        self.difficulty_levels = self._init_difficulty_levels()
        self.current_level = 0
        self.performance_history = []
    
    def _init_difficulty_levels(self) -> List[Dict]:
        """初始化难度级别"""
        return [
            # 级别 0: 新手
            {
                'name': '新手',
                'enemy_strength': 0.5,
                'time_pressure': 0.7,
                'resource_scarcity': 0.3,
                'event_frequency': 0.5
            },
            # 级别 1: 普通
            {
                'name': '普通', 
                'enemy_strength': 0.8,
                'time_pressure': 1.0,
                'resource_scarcity': 0.6,
                'event_frequency': 0.8
            },
            # 级别 2: 困难
            {
                'name': '困难',
                'enemy_strength': 1.2,
                'time_pressure': 1.3,
                'resource_scarcity': 0.8,
                'event_frequency': 1.2
            },
            # 级别 3: 专家
            {
                'name': '专家',
                'enemy_strength': 1.5,
                'time_pressure': 1.6,
                'resource_scarcity': 1.0,
                'event_frequency': 1.5
            },
            # 级别 4: 大师
            {
                'name': '大师',
                'enemy_strength': 2.0,
                'time_pressure': 2.0,
                'resource_scarcity': 1.2,
                'event_frequency': 2.0
            }
        ]
    
    def get_difficulty(self, cycle: int) -> Dict:
        """获取当前难度设置"""
        # 基于周期数的基本难度提升
        base_level = min(cycle // 10, len(self.difficulty_levels) - 1)
        
        # 基于性能的难度调整
        performance_level = self._get_performance_based_level()
        
        # 综合难度级别
        self.current_level = max(base_level, performance_level)
        
        return self.difficulty_levels[self.current_level]
    
    def _get_performance_based_level(self) -> int:
        """基于性能调整难度级别"""
        if len(self.performance_history) < 5:
            return 0
        
        # 计算最近5个周期的平均性能
        recent_performance = np.mean(self.performance_history[-5:])
        
        if recent_performance > 0.8:
            return min(self.current_level + 1, len(self.difficulty_levels) - 1)
        elif recent_performance < 0.3:
            return max(self.current_level - 1, 0)
        else:
            return self.current_level
    
    def update_performance(self, performance: float):
        """更新性能记录"""
        self.performance_history.append(performance)
        
        # 保持历史记录长度
        if len(self.performance_history) > 20:
            self.performance_history.pop(0)
    
    def should_advance(self) -> bool:
        """判断是否应该进入下一难度"""
        if len(self.performance_history) < 10:
            return False
        
        # 检查最近10个周期的性能
        recent_performance = self.performance_history[-10:]
        avg_performance = np.mean(recent_performance)
        
        return avg_performance > 0.7 and self.current_level < len(self.difficulty_levels) - 1